<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/5
 * Time: 上午1:01
 */

namespace Home\Controller;
use Think\Controller;

class RegisterController extends Controller {

    //注册页面

    public function register(){
        if(IS_POST){
            $phone = $_POST['phone'];
            $verfiCode = $_POST['verficode'];
            $passwd = $_POST['passwd'];
            $user_name = $_POST['user_name'];
            $sex = $_POST['sex'];

            if( null == $phone || null == $verfiCode || null == $passwd || null == $sex ){
                $this->error('请完整填写注册信息');
            }
            if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
                $this->error('手机号码错误');
            }

            if( $verfiCode != $_SESSION['verfiCode'] ){
                $this->error('验证码错误');
            }

            //判断手机是否已注册
            $user_db = D('User');
            if($user_db->isPhoneRegister($phone)){
                $this->error('该手机号已经注册');
            }

            //提交信息正确，开始注册
            $user_result = $user_db->toRegister($phone,$passwd,$user_name,$sex);
            //dump($user_result);
            if($user_result){
                $user = $user_db->getByUser_id($user_result);
                session('loginUser',$user);
                //创建简历
                $resume_db = D('Resume');
                $resume_result = $resume_db->setResume($user['user_id']);

                $this->success('注册成功',U('Fetcher'));
            }else{
                $this->error('注册失败');
            }

        }else{
            $this->display('Register/index');
        }
    }

    public function protocol() {
        $this->display();
    }

    public function protocolPC() {
        $this->theme('PC')->display('Register/protocol');
    }

    public function registerPC(){
        if(IS_POST){
            $phone = $_POST['phone'];
            $verfiCode = $_POST['verficode'];
            $passwd = $_POST['passwd'];
            $user_name = $_POST['user_name'];
            $sex = $_POST['sex'];

            if( null == $phone || null == $verfiCode || null == $passwd || null == $sex ){
                $this->error('请完整填写注册信息');
            }
            if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
                $this->error('手机号码错误');
            }

            if( $verfiCode != $_SESSION['verfiCode'] ){
                $this->error('验证码错误');
            }

            //判断手机是否已注册
            $user_db = D('User');
            if($user_db->isPhoneRegister($phone)){
                $this->error('该手机号已经注册');
            }

            //提交信息正确，开始注册
            $user_result = $user_db->toRegister($phone,$passwd,$user_name,$sex);
            //dump($user_result);
            if($user_result){
                $user = $user_db->getByUser_id($user_result);
                session('loginUser',$user);
                //创建简历
                $resume_db = D('Resume');
                $resume_result = $resume_db->setResume($user['user_id']);

                $this->success('注册成功',U('Fetcher'));
            }else{
                $this->error('注册失败');
            }

        }else{
            $this->theme('PC')->display('register');
        }
    }



    //生成手机验证码
    public function getVerfiCode(){

        $result = "";
        for($i = 0;$i < 6;$i++){
            $result .= chr(mt_rand(48,57));
        }
        //dump($result);
        return $result;
    }

    //发送手机验证码
    public function sendVerfiCode(){
        $phone = $_GET['phone'];
        $user_db = D('User');
        if(is_null($phone)){
            $this->error("手机号码为空");
        }else if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
            $this->error("手机号码错误");
        }else if($user_db->isPhoneRegister($phone)){
            $this->error('该手机已注册');
        }else{
            //验证码发送URL
            $url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
            $verfiCode = $this->getVerfiCode();
            $post_data = "account=cf_gwhz&password=XLyxR3&mobile=".$phone."&content=".rawurlencode("您的验证码是：".$verfiCode."。请不要把验证码泄露给其他人。");
            $result = xml_to_array(post($post_data,$url));
            if($result['SubmitResult']['code'] == 2){
                session('verfiCode',$verfiCode);
                $this->success('验证码已发送');
            }else{
                $this->error('验证码发送失败');
            }
        }
    }


    //test
    public function test(){
//        $user_db = D('User');
//        $phone = $_GET['phone'];
//        $passwd = $_GET['passwd'];
//        $user_name = $_GET['user_name'];
//        $sex = $_GET['sex'];
//        $user_result = $user_db->toRegister($phone,$passwd,$user_name,$sex);
//        $user = $user_db->getByUser_id($user_result);
//        dump($user);
    }



} 